#include "pagehisevents.h"
#include "ui_pagehisevents.h"

PageHisEvents::PageHisEvents(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::PageHisEvents)
{
    ui->setupUi(this);
    setAutoFillBackground(true);  //设置自动填充
    QPalette palette = BKGCLR;
    setPalette(palette);
    InitTable();
}

PageHisEvents::~PageHisEvents()
{
    delete ui;
}

void PageHisEvents::InitTable()
{
    m_pHisEventTable = new QTableWidget(this);
    m_pHisEventTable->setGeometry(45,80,1000,600);
    m_pHisEventTable->setStyleSheet("background-color:black;"
                                   "selection-background-color:rgb(215,215,215);"
                                   "selection-color:black;"
                                   "color:white;"
                                   "font-size:14pt;"
                                   "gridline-color:white");
    m_pHisEventTable->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    m_pHisEventTable->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    m_pHisEventTable->setSizeAdjustPolicy(QAbstractScrollArea::AdjustToContents);
    m_pHisEventTable->setSelectionMode(QAbstractItemView::SingleSelection);
    m_pHisEventTable->setSelectionBehavior(QAbstractItemView::SelectRows);
    m_pHisEventTable->setEditTriggers(QAbstractItemView::NoEditTriggers);
    m_pHisEventTable->verticalHeader()->setVisible(false);
    m_pHisEventTable->setRowCount(15);
    m_pHisEventTable->setColumnCount(6);
    QStringList header = QStringList() << "车号" << "故障名称" << "代码" << "等级" << "发生时间" << "消失时间" ;
    QString headerStyle = "QHeaderView::section{ background-color:black; color:white; height:44; font-size:16pt; border: 1px solid white }";
    m_pHisEventTable->setHorizontalHeaderLabels(header);
    m_pHisEventTable->horizontalHeader()->setStyleSheet(headerStyle);
    m_pHisEventTable->horizontalHeader()->setDisabled(true);
    m_pHisEventTable->horizontalHeader()->setFont(QFont("arial"));
    m_pHisEventTable->horizontalHeader()->setSectionResizeMode(0,QHeaderView::Fixed);
    m_pHisEventTable->horizontalHeader()->setSectionResizeMode(1,QHeaderView::Fixed);
    m_pHisEventTable->horizontalHeader()->setSectionResizeMode(2,QHeaderView::Stretch);
    m_pHisEventTable->horizontalHeader()->setSectionResizeMode(3,QHeaderView::Fixed);
    m_pHisEventTable->horizontalHeader()->setSectionResizeMode(4,QHeaderView::Fixed);
    m_pHisEventTable->horizontalHeader()->setSectionResizeMode(5,QHeaderView::Fixed);
    m_pHisEventTable->setFont(QFont("arial"));
    m_pHisEventTable->setColumnWidth(0, 50);
    m_pHisEventTable->setColumnWidth(1, 400);
    m_pHisEventTable->setColumnWidth(3, 50);
    m_pHisEventTable->setColumnWidth(4, 200);
    m_pHisEventTable->setColumnWidth(5, 200);


    m_pHisEventTable->verticalHeader()->setSectionResizeMode(QHeaderView::Stretch);

    for(int i=0; i<m_pHisEventTable->rowCount(); ++i)
    {
        for(int j=0; j<m_pHisEventTable->columnCount(); ++j)
        {
            QTableWidgetItem *item = new QTableWidgetItem(QString::fromUtf8(""));
            if(j != 1)
                item->setTextAlignment(Qt::AlignCenter);
            m_pHisEventTable->setItem(i, j, item);
        }
    }
    connect(m_pHisEventTable, SIGNAL(itemClicked(QTableWidgetItem*)), SLOT(slotItemClicked(QTableWidgetItem*)));
    ui->widgetSolution->hide();
}

void PageHisEvents::showHisEventHint()
{
    ui->widgetSolution->show();
    m_pHisEventTable->hide();
}

void PageHisEvents::on_btnBack_clicked()
{
    ui->widgetSolution->hide();
    m_pHisEventTable->show();
}

void PageHisEvents::slotItemClicked(QTableWidgetItem*)
{
    if(m_pHisEventTable->selectedItems().isEmpty())
        return;
    int row = m_pHisEventTable->currentRow();   //获取当前点击的是哪一行
    ui->HisFaultCar->setText(m_pHisEventTable->item(row,0)->text()+"车");
    ui->HisFaultDescription->setText(m_pHisEventTable->item(row,1)->text());
    ui->HisFaultLevel->setText(m_pHisEventTable->item(row,3)->text());
    ui->HisFaultCode->setText(m_pHisEventTable->item(row,2)->text());
    ui->HisFaultStartTime->setText(m_pHisEventTable->item(row,4)->text());
    ui->HisFaultEndTime->setText(m_pHisEventTable->item(row,5)->text());
    refreshHint();
}

void PageHisEvents::refreshHint()
{
    int row = m_pHisEventTable->currentRow();
    if(row < 0)
    {
        return;
    }
    int page = ui->currentPage->text().toInt();
    int index = ui->historyCount->text().toInt() - 1 - row - (page-1)*m_pHisEventTable->rowCount();
    if(index < 0)
    {
        return;
    }
    QString str = QString();
    EventData data;
    if(GlobalConfig::getHistoryData(index, data) == 0)
    {
        str = data.event.hint;
        str.replace("；", "\n");
    }
    ui->LableSolution->setText(str);
}

void PageHisEvents::hideHintInfo()
{
    ui->widgetSolution->hide();
    m_pHisEventTable->show();
}

void PageHisEvents::on_btnNext_clicked()
{
    int curpage = ui->currentPage->text().toInt();
    int totalpage = ui->totalPage->text().toInt();
    if(curpage < totalpage)
    {
        ui->currentPage->setText(QString().setNum(++curpage));
        updateTable();
    }
}

void PageHisEvents::on_btnPrev_clicked()
{
    int curpage = ui->currentPage->text().toInt();
    if(curpage > 1)
    {
        ui->currentPage->setText(QString().setNum(--curpage));
        updateTable();
    }
}

void PageHisEvents::updateHistory()
{
    int count = GlobalConfig::historyData.size();
    int rowCount = m_pHisEventTable->rowCount();
    int page = (count%rowCount) ? (count/rowCount+1) : (count/rowCount);
    ui->totalPage->setText(QString().setNum(page));
    if(ui->currentPage->text().toInt() == 0 && page > 0)
        ui->currentPage->setText("1");
    else if(ui->currentPage->text().toInt() > page)
        ui->currentPage->setText(QString().setNum(page));
    ui->historyCount->setText(QString().setNum(count));
    updateTable();
}

void PageHisEvents::updateTable()
{
    int page = ui->currentPage->text().toInt();
    if(page == 0)
    {
        for(int i=0; i<m_pHisEventTable->rowCount(); ++i)
            for(int j=0; j<m_pHisEventTable->columnCount(); ++j)
                m_pHisEventTable->item(i, j)->setText("");
        return;
    }
    for(int i=0; i<m_pHisEventTable->rowCount(); ++i)
    {
        EventData data;
        int index = ui->historyCount->text().toInt() - 1 - i - (page-1)*m_pHisEventTable->rowCount();
        if(index < 0)
            for(int j=0; j<m_pHisEventTable->columnCount(); ++j)
                m_pHisEventTable->item(i, j)->setText("");
        else if(GlobalConfig::getHistoryData(index, data) == 0)
        {
            m_pHisEventTable->item(i, 0)->setText(data.event.car);
            m_pHisEventTable->item(i, 1)->setText(data.event.description);
            m_pHisEventTable->item(i, 2)->setText(data.event.code);
            m_pHisEventTable->item(i, 3)->setText(data.event.level);
            QString str = QDateTime::fromTime_t(data.startTime).toString("MM-dd hh:mm:ss");
            m_pHisEventTable->item(i, 4)->setText(str);
            str = QDateTime::fromTime_t(data.endTime).toString("MM-dd hh:mm:ss");
            m_pHisEventTable->item(i, 5)->setText(str);
        }
    }
}
